package com.cskaoyan.exceptionhandler;

import com.cskaoyan.bean.CskaoyanmallLog;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.mapper.CskaoyanmallLogMapper;
import com.cskaoyan.myenum.ResultEnum;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author Fantasylkh
 */
@RestControllerAdvice
public class LoginHandler {

    @Autowired
    CskaoyanmallLogMapper logMapper;

    @ExceptionHandler(value = {UnknownAccountException.class, IncorrectCredentialsException.class})
    public BaseRespVo login(Exception exception, HttpServletRequest request) {
        Date date = new Date();
        CskaoyanmallLog log = new CskaoyanmallLog();
        log.setAddTime(date);
        log.setUpdateTime(date);
        log.setDeleted(false);
        log.setAction("登录");
        log.setType(1);
        log.setAdmin("匿名用户");
        log.setStatus(false);
        log.setResult(exception.getMessage());
        log.setIp(request.getRemoteAddr());

        logMapper.insert(log);


        return new BaseRespVo(ResultEnum.ACCOUNT_FAIL);
    }
}
